#include "config.h"

#define countof(a) (sizeof(a) / sizeof(*(a)))
#define TxbufferSize (countof(TxBuffer) - 1)
#define RxbufferSize (countof(RxBuffer) - 1)
uint8_t TxBuffer[] = "abc";
uint8_t RxBuffer[] = "";
__IO uint8_t RxCounter = 0x00;
__IO uint8_t TxCounter = 0x00;
uint8_t NbrOfDataToRead = RxbufferSize;
uint8_t NbrOfDataToTransfer = TxbufferSize;

volatile uint16_t low_adc_ready=0;
volatile uint16_t low_adc_value=0;

// 内部时钟HSI为系统时钟，频率64HMz
#define DutyRatio_Low 10        // 低压电机占空比
#define LOW_FIXED_PERIOD_MS 1000 // 读取低压压力传感器时间间隔（单位ms）

int main()
{
    RCC_ClocksTypeDef RCC_Clocks;
    RCC_GetClocksFreq(&RCC_Clocks);

    LED_GPIO_CONFIG();
    LED_ON();

    // USART_RCC_CONFIG();
    // USART_NVIC_CONFIG();
    // USART_GPIO_CONFIG();

    // USART_CONFIG();
    // USART_Cmd(USART3, ENABLE);

    // uint32_t i = 12000 * 500;
    // while (i--)
    // {
    // }

    // USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
    // USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    // char *a = "1";
    // for (i = 0; i < TxbufferSize; i++)
    // {
    //     TxBuffer[i] = *a;
    // }
    // USART_ITConfig(USART3, USART_IT_TXE, ENABLE);

    // PWM1 控制低压电机
    PWM1_GPIO_CONFIG();
    PWM1_TIM1_CONFIG();
    TIM_Cmd(TIM1, ENABLE);

    TIM_SetCompare1(TIM1, DutyRatio_Low * 10); // 设置PWM1初始占空比  DutyRatio1=50 (50*10) / 1000 = 50%

    // ADC1 读取低压压力传感器检测值
    NVIC_Configuration(); 
    ADC1_CONFIG();   
    TIM2_Configuration(LOW_FIXED_PERIOD_MS);

   

    float voltage1;
    while (1)
    {
       // 检测ADC转换完成标志
       if (low_adc_ready)
       {
           // 将ADC值转换为电压值（假设参考电压为3.3V）
           voltage1 = (low_adc_value * 3.3f) / 4095.0f;

           // 清除ADC转换完成标志
           low_adc_ready = 0;
       }
    }
}